#!/bin/bash

# 临时禁用IPv4源路由
echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
echo 0 > /proc/sys/net/ipv4/conf/default/accept_source_route

# 临时禁用IPv6源路由
echo 0 > /proc/sys/net/ipv6/conf/all/accept_source_route 2>/dev/null
echo 0 > /proc/sys/net/ipv6/conf/default/accept_source_route 2>/dev/null

# 永久禁用IPv4源路由
if grep -q "^net.ipv4.conf.all.accept_source_route" /etc/sysctl.conf; then
    sed -i 's/^net.ipv4.conf.all.accept_source_route.*/net.ipv4.conf.all.accept_source_route=0/' /etc/sysctl.conf
else
    echo "net.ipv4.conf.all.accept_source_route=0" >> /etc/sysctl.conf
fi

if grep -q "^net.ipv4.conf.default.accept_source_route" /etc/sysctl.conf; then
    sed -i 's/^net.ipv4.conf.default.accept_source_route.*/net.ipv4.conf.default.accept_source_route=0/' /etc/sysctl.conf
else
    echo "net.ipv4.conf.default.accept_source_route=0" >> /etc/sysctl.conf
fi

# 永久禁用IPv6源路由
if grep -q "^net.ipv6.conf.all.accept_source_route" /etc/sysctl.conf; then
    sed -i 's/^net.ipv6.conf.all.accept_source_route.*/net.ipv6.conf.all.accept_source_route=0/' /etc/sysctl.conf
else
    echo "net.ipv6.conf.all.accept_source_route=0" >> /etc/sysctl.conf
fi

if grep -q "^net.ipv6.conf.default.accept_source_route" /etc/sysctl.conf; then
    sed -i 's/^net.ipv6.conf.default.accept_source_route.*/net.ipv6.conf.default.accept_source_route=0/' /etc/sysctl.conf
else
    echo "net.ipv6.conf.default.accept_source_route=0" >> /etc/sysctl.conf
fi

# 应用配置
sysctl -p >/dev/null 2>&1

echo "源路由数据包禁用配置完成"